﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.IO;
using System.Xml.Serialization;

namespace ZhangZhao.WebAPI
{
    public partial class UserGatherThirdPhotoComment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string s = string.Empty;

            try
            {
                string UserName = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.UserName]);
                string Output = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.Output]);
                string Command = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.Command]);
                int ThirdPhotoId = 0;
                int.TryParse(HttpContext.Current.Request[Parameter.ThirdPhotoId], out ThirdPhotoId);

                string where = string.Empty;

                if (string.IsNullOrWhiteSpace(Command) || Command.Equals(CommandParameterValue.Single, StringComparison.CurrentCultureIgnoreCase))
                {
                    #region 获取收藏的某一张图片的全部评论
                    if (ThirdPhotoId > 0)
                    {
                        where = string.Format(" UserGatherThirdPhotoId = {0} ", ThirdPhotoId);       
                    }
                    #endregion
                }
                if (!string.IsNullOrWhiteSpace(Command) && Command.Equals(CommandParameterValue.All, StringComparison.CurrentCultureIgnoreCase))
                {
                    #region 获取此用户收藏的所有图片的全部评论
                    if (!string.IsNullOrWhiteSpace(UserName))
                    {
                        where = string.Format(" UserGatherThirdPhotoId in ( select Id from H_UserGatherThirdPhoto with (nolock) where UserName = '{0}' ) ", UserName);
                    }
                    #endregion
                }


                ZhangZhao.BLL.H_UserGatherThirdPhotoComment bll = new ZhangZhao.BLL.H_UserGatherThirdPhotoComment();
                List<ZhangZhao.Model.H_UserGatherThirdPhotoComment> list = bll.GetModelList(where);

                if (list != null && list.Count > 0)
                {
                    if (!string.IsNullOrWhiteSpace(Output) && Output.Equals("json", StringComparison.CurrentCultureIgnoreCase))
                    {
                        s = JsonConvert.SerializeObject(list);
                    }
                    else
                    {
                        using (MemoryStream Stream = new MemoryStream())
                        {
                            XmlSerializer xml = new XmlSerializer(typeof(List<ZhangZhao.Model.H_UserGatherThirdPhotoComment>));

                            try
                            {
                                xml.Serialize(Stream, list);
                            }
                            catch { }

                            Stream.Position = 0;
                            using (StreamReader sr = new StreamReader(Stream))
                            {
                                s = sr.ReadToEnd();
                            }

                            xml = null;
                        }
                    }
                }
            }
            catch { }

            Response.Clear();
            Response.Write(s);
            Response.End();
        }
    }
}